package com.fare.taluo.application.impl;

import java.io.OutputStream;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.fare.taluo.util.excel.engine.ExcelParseFactory;
import com.fare.taluo.vo.core.AnalyseBaseVo;
import com.fare.taluo.vo.core.LowestPriceCompareVo;
import com.fare.taluo.vo.core.PlaneInfo;
import com.fare.taluo.application.AnalyseApplication;

public class LowestPriceCompareApplicationImpl extends AnalyseApplication{

	
	public LowestPriceCompareVo analyse2(Map<String, PlaneInfo> firstMap, Map<String, PlaneInfo> secondMap){

		Integer firstPrice = 0;
		PlaneInfo firstInfo = null;
		for(String key: firstMap.keySet()){
			firstInfo = firstMap.get(key);
			if(firstInfo != null)
				firstPrice = Integer.valueOf(((PlaneInfo) firstMap.get(key)).getLowprPrice());
		}
		
		PlaneInfo secondInfo = null;
		Integer secondPrice = 0;
		for (Iterator iterator = secondMap.keySet().iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			if(secondMap != null && secondMap.get(key) != null){
				secondInfo = secondMap.get(key);
				String lowp = secondMap.get(key).getLowprPrice();
				secondPrice = Integer.valueOf(lowp.indexOf(".")>0 ? lowp.substring(0,lowp.indexOf(".")) : lowp);
			}
		}
		String finalDiscount = "";
		if(firstPrice != 0 && secondPrice != 0){
			double discount = ((firstPrice+0.0) - (secondPrice+0.0))/(firstPrice+0.0);
			NumberFormat nf = NumberFormat.getInstance();
			nf.setMinimumFractionDigits(2);
			finalDiscount = nf.format(discount * 100) + "%";
		}
		
		LowestPriceCompareVo result = null;
		try{
			result = new LowestPriceCompareVo(firstInfo, secondInfo, finalDiscount);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}



	@Override
	public void toExcel(List vos, OutputStream os) {
		Map<String, String> contextMap = new HashMap<String, String>();
		Map<String, List> dataMap = new HashMap<String, List>();
		dataMap.put("PriceQuery", vos);
		ExcelParseFactory.writeExcelNoCalculate("LowestPriceCompare", dataMap, contextMap, os);
	}
	

}
